<?php
defined('BASEPATH') OR exit('No direct script access allowed');

?>


<div class="row">
                        <div class="col-sm-12">
                             <div class="box">
                                <div class="box-header with-border">
                                    <div class=" col-sm-6">
                                        <?php echo form_open(current_url(), array('class' => 'form-inline', 'method'=>'get', 'id' => 'search_goods')); ?>                                    
                                        
                                        <div class="form-group" style="margin-right:15px;">
                                       
                                        <div class="input-group input-group-sm">
                                                <?php echo form_input($keyword);?>
                                                <span class="input-group-btn">
                                         
                                                </span>
                                        </div>                                        
                                        </div>
                                        <div class="form-group" >
                                            <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-sm btn-primary btn-flat', 'content' => "搜索任务")); ?>    <span class="btn btn-sm btn-success btn-flat" id="viewtask">查看任务</span>
                                        </div>

                                        <div class="form-group" >
                                            <span class="btn btn-sm btn-danger btn-flat" id="pushjob">立即推送</span>
                                        </div>
                                        
                                        <?php echo form_close();?>
                                    </div>
                                    <h3 class="box-title pull-right"><?php //echo anchor('admin/tbshop/create', '<i class="fa fa-plus"></i> 添加目录', array('class' => 'btn btn-block btn-sm btn-primary btn-flat')); ?></h3>
                                </div>
                                <div class="box-body">
                                       
                                    <table class="table  table-hover" style="margin-top:20px;">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>任务类别</th>
                                                <th>任务状态</th>
                                                <th width="200">完成进度</th>
                                                <th width="500">备注</th>
                                                <th>添加时间</th>
                                                <th>开始时间</th>
                                                <th>完成时间</th>
                                             
                                                <th></th>
                                              
                                            </tr>
                                        </thead>
                                        <tbody>
<?php foreach ($data as $item):?>
<tr shopid="<?php echo $item["id"]; ?>">
    <td><?php echo $item["id"]; ?></td>
    <td><i class="fa fa-circle-o text-red"></i> <span><?php echo $jobtype[$item["job_type"]]; ?></span></td>
    <td><span class="label <?php echo $statuslabel[$item["status"]]; ?>"><?php echo $statustype[$item["status"]]; ?></span></td>
    <td>
        <?php
            
            $width=intval($item["donecount"]/$item["totals"]*100);
           if($width==0) $width=0;
        ?>
        <div class="progress">
                <div class="progress-bar progress-bar-green progress-bar-striped" role="progressbar" aria-valuenow="<?php echo $width;?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $width;?>%"></div>
                
                <div style='color:#333;text-align:center;position:absolute;width:190px;'><?php echo $item["donecount"]."/".$item["totals"]; ?></div>
        </div>        
    </td>
    <td><?php echo $item["note"]; ?></td>
    <td><?php echo date("m-d H:i",strtotime($item["addtime"])); ?></td>
    <td><?php echo $item["starttime"]?date("m-d H:i:s",strtotime($item["starttime"])):"尚未开始"; ?></td>
    <td><?php echo $item["endtime"]=="0000-00-00 00:00:00"?"等待完成":date("m-d H:i:s",strtotime($item["endtime"])); ?></td>
    <td><?php if ($item["status"] == 0): ?><span class="btn btn-danger btn-xs btn-cancel"
                                                 itemid="<?php echo $item["id"]; ?>">
                取消任务</span>&nbsp;&nbsp;<?php endif; ?> <?php if (in_array($item["status"], [1, 2])&&($item["donecount"]!=$item["totals"])): ?><span
            class="btn btn-info btn-xs btn-reset" itemid="<?php echo $item["id"]; ?>">重置任务</span> <?php endif; ?></td>
</tr>
<?php endforeach;?>
                                        </tbody>
                                    </table>
                                    <?php 
                                    if(empty($data)){
                                        echo "<h2 style='text-align:center;padding:50px 0;'>没有查询到数据</h2>";
                                    }
                                    ?>
                                    <?php echo $pager;?>
                                </div>
                            </div>
                         </div>
                    </div>
        
<script>


var loadtask=function()
{
    $("tbody#tasklist tr").each(function(){
        if($(this).attr("id")=="viewinfo")
        {
            $(this).show().find("td").text("数据加载中...");
        }
        else
        {
            $(this).remove();
        }
    });
    $.getJSON("/backend/crons/ajaxlistcron/",function(result){
        if(result["data"])
        {
            $("tr#viewinfo").hide();
            $.each(result["data"], function(i, item){
                $("tbody#tasklist").append('<tr><td>'+item["pid"]+'</td><td>'+item["stime"]+'</td><td><span class="btn btn-xs btn-flat btn-danger stoptask" pid="'+item["pid"]+'">停止任务</span></td></tr>');
            });
        }
        else
        {
            $("tr#viewinfo td").text('暂时没有运行中的任务');
        }
    });
}
$("span.stoptask").on("click",function(){
    let pid=$(this).attr("pid");
    shadow = layer.load(1, {
        shade: [0.1,'#000'] 
    });
    $.post("/backend/crons/killcron",{"pid":pid},function(result){
        layer.close(shadow);
        loadtask();          
    },dataType="json");
});

$("span#viewtask").on("click",function(){
    layer.open({
        type: 1,
        title: '查看运行的任务',
        closeBtn: 1,
        offset : '100px',
        shadeClose: true,
        area:["500px","300px"],
        skin: 'layui-layer-lan',
        content: '<div style="padding:20px 30px;"><table class="table table-hover"><thead><tr><th>任务PID</th><th>开始时间</th><th>操作</th></tr></thead><tbody id="tasklist"><tr id="viewinfo"><td colspan="3">数据加载中</td></tr></tbody></table></div>'
    });
    loadtask();
});

$("span#pushjob").on("click",function(){
    layer.confirm("确认推送任务吗？", { title: "确认" }, function (index) {
        shadow = layer.load(1, {
            shade: [0.8, '#393D49']
        });
        $.ajax({
            type:"post",
            url:"/backend/crons/immediately_do_job",
            dataType:"json",
            data:{},
            error:function(){
                layer.close(shadow);
            },
            success:function(result){
                if(result["status"]==0)
                {
                    window.location.href=window.location.href;
                }
                else
                {
                    layer.msg(result["msg"]);
                    window.location.href=window.location.href;
                }
                layer.closeAll();
            },
        });
    });
});

$("span.btn-reset").on("click",function(event){
    var cid=$(this).attr("itemid");
    layer.confirm("确认重置任务吗？", { title: "确认" }, function (index) {           
        $.ajax({
            type:"post",
            url:"/backend/crons/resetjob",
            dataType:"json",
            data:{"cid":cid},
            error:function(){
            },
            success:function(result){
                if(result["status"]==1)
                {
                    window.location.href=window.location.href;
                }
                else
                {
                    layer.msg(result["msg"]);
                }
            },
        });
        layer.close(index); 
    });
    
});


$("span.btn-cancel").on("click",function(event){
    var cid=$(this).attr("itemid");
    layer.confirm("确认取消任务吗？", { title: "确认" }, function (index) {           
        $.ajax({
            type:"post",
            url:"/backend/crons/canceljob",
            dataType:"json",
            data:{"cid":cid},
            error:function(){
            },
            success:function(result){
                if(result["status"]==1)
                {
                    window.location.href=window.location.href;
                }
                else
                {
                    layer.msg(result["msg"]);
                }
            },
        });
        layer.close(index); 
    });
    
});
</script>
